home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / Misc. C ƒ / Recorder / Old / Ansych Recorder.c #2 next >
Encoding:
Text File  |  1990-06-04  |  5.6 KB  |  238 lines  |  [TEXT/MPS ]

  1. /*    File: Recorder.c
  2.  
  3.     MPW Tool for recording serial line traffic
  4.     
  5. */
  6.  
  7.  
  8. #include <Types.h>
  9. #include <QuickDraw.h>
  10. #include <CursorCtl.h>
  11.  
  12. #include <CRMIntf.h>
  13. #include <CTBUtils.h>
  14. #include <CMIntf.h>
  15. #include <FTIntf.h>
  16. #include <TMIntf.h>
  17.  
  18. #include <StdIO.h>
  19. #include <StdLib.h>
  20.  
  21. pascal void SetupGlob ( void );
  22. pascal void ReadCompletionGlue1  ( ConnHandle hConn );
  23. pascal void ReadCompletionGlue2  ( ConnHandle hConn );
  24. pascal void WriteCompletionGlue1 ( ConnHandle hConn );
  25. pascal void WriteCompletionGlue2 ( ConnHandle hConn );
  26.  
  27. #define    CommToolBoxTrap        0x8B
  28. #define    UnimplementedTrap    0x9F
  29. #define    Check(err,str)    { \
  30.                         OSErr errXYZZY; \
  31.                         if (( errXYZZY = ( err )) != noErr ) \
  32.                             fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
  33.                         }
  34. #define    OUTCONFIGSTR    "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
  35.                         "Handshake None HoldConnection False RemindDisconnect False"
  36. #define    INCONFIGSTR        "Baud 2400 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
  37.                         "Handshake None HoldConnection False RemindDisconnect False"
  38.  
  39. #define        BUF_SIZE    1024
  40.  
  41. short        procID1;
  42. short        procID2;
  43. ConnHandle    stream1;
  44. ConnHandle    stream2;
  45. char        buffer1    [ BUF_SIZE ];
  46. char        buffer2    [ BUF_SIZE ];
  47. long        readSize1;
  48. long        writeSize1;
  49. long        readSize2;
  50. long        writeSize2;
  51. CMFlags        inFlags1;
  52. CMFlags        inFlags2;
  53. CMFlags        outFlags1;
  54. CMFlags        outFlags2;
  55.  
  56. /*    Is the Comm Toolbox actually installed ?? */
  57. Boolean        IsCTBInstalled    ( ) {
  58.     return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
  59.             NGetTrapAddress ( CommToolBoxTrap, OSTrap );
  60.     }
  61.  
  62. short    InitAll ( void ) {
  63.     OSErr    err;
  64.  
  65.     InitGraf ( &qd.thePort );
  66. /*
  67.     InitFonts ();
  68.     InitWindows ();
  69.     InitMenus ();
  70.     TEInit ();
  71.     InitDialogs ( NULL );
  72.     InitCursor ();
  73. */
  74.  
  75.     InitCursorCtl ( NULL );
  76.     SetupGlob ();
  77.     
  78.     if ( !IsCTBInstalled ) {
  79.         fprintf ( stderr, "Comm Toolbox not installed!\n" );
  80.         return 1;
  81.         }
  82.  
  83. /*    Load up the Communications Toolbox */
  84.     (void) InitCTBUtilities ();
  85.     (void) InitCRM ();
  86.  
  87.     err = InitTM ();
  88.     if ( err == tmNoTools ) {
  89.         fprintf ( stderr, "No terminal tools found\n" );
  90.         return 2;
  91.         }
  92.     
  93.     err = InitCM ();
  94.     if ( err == cmNoTools ) {
  95.         fprintf ( stderr, "No connection tools found\n" );
  96.         return 2;
  97.         }
  98.     
  99.     err = InitFT ();
  100.     if ( err == ftNoTools ) {
  101.         fprintf ( stderr, "No file transfer tools found\n" );
  102.         return 2;
  103.         }        
  104.  
  105.     return 0;
  106.     }
  107.  
  108.  
  109. void ExitProc ( void ) {
  110.     DebugStr ( "\pExit" );
  111.     
  112. /*    Close the connection and dispose of the connection record */
  113.     if ( stream1 != NULL ) {
  114.         Check ( CMClose ( stream1, false, NULL, -1, true ), "CMCLose - input" );
  115.         CMDispose ( stream1 );
  116.         }
  117.  
  118.     if ( stream2 != NULL ) {
  119.         Check ( CMClose ( stream2, false, NULL, -1, true ), "CMCLose - output" );
  120.         CMDispose ( stream2 );
  121.         }
  122.     }
  123.  
  124. ConnHandle    InitStream ( short *procID ) {
  125.     CMBufferSizes    bSize;
  126.  
  127. /*    Open a connection tool */
  128.     bSize [ cmDataIn ] = BUF_SIZE;        bSize [ cmDataOut ] = BUF_SIZE;
  129.     bSize [ cmCntlIn ] = 0;                bSize [ cmCntlOut ] = 0;
  130.     bSize [ cmAttnIn ] = 0;                bSize [ cmAttnOut ] = 0;
  131.     bSize [ cmRsrvIn ] = 0;                bSize [ cmRsrvOut ] = 0;
  132.     *procID = CMGetProcID ( "\pSerial" );
  133.     return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
  134.     }
  135.                             
  136.  
  137. /*    Interrupt routine */
  138. pascal void ReadCompletion1 ( ConnHandle hConn ) {
  139.     
  140.     if ((*hConn)->errCode == noErr ) {
  141.         writeSize1 = (*hConn)->asyncCount [ cmDataIn ];
  142.         CMWrite ( stream2, buffer1, &writeSize1, 
  143.                     cmData, true, (ProcPtr) WriteCompletionGlue1, 0, inFlags1 );
  144.         }
  145.     }
  146.  
  147.  
  148. /*    Interrupt routine */
  149. pascal void WriteCompletion1 ( ConnHandle hConn ) {
  150.     
  151. /*    re-enable the read */
  152.     if ((*hConn)->errCode == noErr ) {
  153.         readSize1 = 1;
  154.         CMRead ( stream1, buffer1, &readSize1,
  155.                 cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags2 );
  156.         }
  157.     }
  158.  
  159.  
  160. /*    Interrupt routine */
  161. pascal void ReadCompletion2 ( ConnHandle hConn ) {
  162.  
  163.     if ((*hConn)->errCode == noErr ) {
  164.         writeSize2 = (*hConn)->asyncCount [ cmDataIn ];
  165.         CMWrite ( stream1, buffer2, &writeSize2, 
  166.                     cmData, true, (ProcPtr) WriteCompletionGlue2, 0, inFlags2 );
  167.         }
  168.     }
  169.  
  170.  
  171. /*    Interrupt routine */
  172. pascal void WriteCompletion2 ( ConnHandle hConn ) {
  173.  
  174. /*    re-enable the read */
  175.     if ((*hConn)->errCode == noErr ) {
  176.         readSize2 = 1;
  177.         CMRead ( stream2, buffer2, &readSize2,
  178.                 cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags1 );
  179.         }
  180.     }
  181.  
  182.  
  183. int main ( int argc, char *argv[] ) {
  184. #pragma unused ( argc )
  185. #pragma unused ( argv )
  186.     short    err;
  187.     long    cnt;
  188.     
  189.     if ( err = InitAll ( ) != 0 )
  190.         exit ( err );
  191.         
  192.     atexit ( ExitProc );
  193.  
  194. /*    Open a connection tool */
  195.     stream1 = InitStream ( &procID1 );
  196.     if ( stream1 == NULL ) {
  197.         fprintf ( stderr, "Cannot create input handle\n" );
  198.         return 3;
  199.         }
  200.         
  201. /*    Open another connection tool */
  202.     stream2 = InitStream ( &procID1 );
  203.     if ( stream2 == NULL ) {
  204.         fprintf ( stderr, "Cannot create output handle\n" );
  205.         return 3;
  206.         }
  207.  
  208. /*    Configure the connection */
  209.     Check ( CMSetConfig ( stream1, INCONFIGSTR ), "CMSetConfig - In" );
  210.     Check ( CMOpen ( stream1, false, NULL, -1 ), "CMOpen - In" );
  211.     Check ( CMListen ( stream1, false, NULL, -1 ), "CMListen - In" );
  212.     
  213.     Check ( CMSetConfig ( stream2, OUTCONFIGSTR ), "CMSetConfig - Out" );
  214.     Check ( CMOpen ( stream2, false, NULL, -1 ), "CMOpen - Out" );
  215.     Check ( CMListen ( stream2, false, NULL, -1 ), "CMListen - Out" );
  216.  
  217. /*    Do an ansych read on the input stream */
  218.     readSize1 = 1;
  219.     Check ( CMRead ( stream1, buffer1, &readSize1,
  220.                                 cmData, true, (ProcPtr) ReadCompletionGlue1, 0, &inFlags1 ), "CMRead" );
  221.     readSize2 = 1;
  222.     Check ( CMRead ( stream2, buffer2, &readSize2,
  223.                                 cmData, true, (ProcPtr) ReadCompletionGlue2, 0, &inFlags2 ), "CMRead" );
  224.     cnt = 0;
  225.     while ( true ) {
  226.         
  227.         cnt++;
  228.         if ( cnt % 256 == 0 ) {
  229.             CMIdle ( stream1 );
  230.             CMIdle ( stream2 );
  231.             SpinCursor ( 1 );
  232.             }
  233.             
  234.         }
  235.  
  236.     return 0;
  237.     }
  238.